<template>
  <el-card class="list-page">
    <div class="list-h">
      <div>
        <el-button
          type="primary"
          @click="handleDownload('bath')"
          v-auth="`/projectsB/approval--bath`"
          >批量下载</el-button
        >
      </div>

      <el-form
        :inline="true"
        :model="formInline"
        ref="formRef"
        class="demo-form-inline"
      >
        <el-form-item label="文档名称" prop="documentNameLike">
          <el-input
            class="width-custom120"
            v-model="formInline.documentNameLike"
          />
        </el-form-item>

        <el-form-item label="所属阶段" prop="projectStageNameLike">
          <el-input
            class="width-custom120"
            v-model="formInline.projectStageNameLike"
          />
        </el-form-item>

        <el-form-item label="所属事项" prop="projectTaskNameLike">
          <el-input
            class="width-custom120"
            v-model="formInline.projectTaskNameLike"
          />
        </el-form-item>

        <el-form-item label="文件类型" prop="docType">
          <el-select
            clearable
            class="width-custom120"
            v-model="formInline.docType"
          >
            <el-option
              v-for="item in docTypeList"
              :label="item.dicValue"
              :key="item.dicCode"
              :value="item.dicCode"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="创建人" prop="createUserNameLike">
          <el-input
            class="width-custom120"
            v-model="formInline.createUserNameLike"
          />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            class="width-custom220"
            v-model="timeValue"
            type="daterange"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="handleChangeDate"
          />
        </el-form-item>

        <el-form-item>
          <el-button @click="handleReset">重置</el-button>
          <el-button
            type="primary"
            @click="handleSearch"
            v-auth="`/projectsB/approval--query`"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <el-skeleton style="height: 50vh" :rows="8" :loading="loading" animated>
      <el-table
        :data="tableData"
        stripe
        style="width: 100%"
        v-loading="loading"
        :header-cell-style="{
          background: '#F7F9FD',
          color: '#000',
          height: '48px',
          borderTop: '1px soild #E4E8F2'
        }"
        :row-class-name="tableRowClassName"
      >
        <template #empty>
          <el-empty :image-size="150" description="暂无数据"></el-empty>
        </template>

        <el-table-column type="index" label="序号" width="80">
          <template v-slot="scope">
            <span>{{
              (pageState.pageNum - 1) * pageState.pageSize + scope.$index + 1
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="documentName" label="文档名称">
        </el-table-column>
        <el-table-column prop="projectStageName" label="所属阶段">
        </el-table-column>
        <el-table-column prop="projectTaskName" label="所属事项">
        </el-table-column>
        <el-table-column prop="docTypeName" label="文件类型"> </el-table-column>
        <el-table-column prop="createUserName" label="创建人">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间"> </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template #default="{ row }">
            <el-button
              type="text"
              size="small"
              @click="handlePreview(row)"
              v-auth="`/projectsB/approval--preview`"
              >预览</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="handleDownload('down', row)"
              v-auth="`/projectsB/approval--downLoad`"
              >下载</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <re-pagination
        v-if="total > 0"
        :total="total"
        v-model:page="pageState.pageNum"
        v-model:limit="pageState.pageSize"
        @pagination="handleChangePage"
      />
    </el-skeleton>
  </el-card>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import { useRouter } from "vue-router";
import RePagination from "/@/components/RePagination/index.vue";
import { download } from "/@/utils/download";
import * as api from "/@/api/document";
import { storageLocal } from "/@/utils/storage";
const formRef = ref<HTMLFormElement | null>(null) as any;
const initData = {
  // TODO 接口未出，后续根据字段直接替换
  documentNameLike: "",
  projectStageNameLike: "", //项目阶段
  projectTaskNameLike: "", //项目事项名称
  docType: "", //
  moduleType: "2",
  createUserNameLike: "", //
  createTimeStrStart: "", //
  createTimeStrEnd: "", //
  projectManageId: storageLocal.getItem("projectInfo")?.id
};

const formInline = reactive(initData);
const timeValue = ref(null);
const docTypeList = ref([]);
const tableData = ref([]);
const pageState = reactive({
  pageNum: 1,
  pageSize: 10
});

const router = useRouter();
const loading = ref<boolean>(false);
const total = ref<number>(0);
const formParams = ref<typeof initData>({ ...initData });

//获取数据列表
const getTableList = async (obj: object = {}) => {
  try {
    loading.value = true;
    const { code, data } = await api.fileList({
      ...obj,
      ...pageState
    });
    if (code === 200) {
      tableData.value = data.list;
      total.value = data.rows;
    }
  } catch (err) {
    console.log(err);
  } finally {
    loading.value = false;
  }
};

//改变时间
const handleChangeDate = item => {
  if (item) {
    formInline.createTimeStrStart = item[0];
    formInline.createTimeStrEnd = item[1];
  } else {
    formInline.createTimeStrStart = "";
    formInline.createTimeStrEnd = "";
  }
};

const handleReset = () => {
  formRef.value.resetFields();
  timeValue.value = null;
  handleChangeDate(null);
  formParams.value = initData;
  getTableList(formInline);
};

const handleSearch = () => {
  pageState.pageNum = 1;
  formParams.value = formInline;
  getTableList(formInline);
};

const handleChangePage = () => {
  getTableList(formParams.value);
};

const handleDownload = (type, row) => {
  if (type === "down") {
    const item = row.documentAttachmentArr[0];
    if (!item) return;
    download(item);
  } else if (type === "bath") {
    router.replace({
      path: "/projectsB/document/manage/download",
      query: {
        mode: "approval"
      }
    });
  }
};

const handlePreview = row => {
  window.open(
    `${location.origin}${location.pathname}#/preview?uuidFile=${row.documentAttachmentArr[0].uuidFile}&fileName=${row.documentAttachmentArr[0].fileName}`
  );
};

// 获取文件类型
const initDocTypeList = async () => {
  try {
    const { data } = await api.postDictList({ dicType: "thd_doc_type" });
    docTypeList.value = data;
  } catch (err) {
    console.log(err);
  }
};

const tableRowClassName = ({ rowIndex }) => {
  if (rowIndex % 2 == 1) {
    return "odd-row";
  } else {
    return "";
  }
};

onMounted(() => {
  initDocTypeList();
});
getTableList(formInline);
</script>
<style scoped lang="scss">
.redColor {
  width: 10px;
  height: 10px;
  background: #f5a623;
  border-radius: 50%;
  display: inline-block;
}
.greenColor {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #21d371;
  display: inline-block;
}
.grayColor {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #979797;
  display: inline-block;
}
:deep .el-dialog__body {
  padding: 20px 40px 8px 10px;
}
:deep .el-table__row.odd-row {
  background: #fdfdff;
}
</style>
